local extension = Package:new("jianzhong")
extension.extensionName = "king"
local U = require "packages/utility/utility"


Fk:loadTranslationTable{
    ["jianzhong"] = "剑冢",
  }


local chengui = General(extension, "jianzhong1", "qun", 4)
  local jianzhong__1 = fk.CreateTriggerSkill{
    name = "jianzhong__1",
    anim_type = "control",
    events = {fk.AfterCardsMove},
    can_trigger = function(self, event, target, player, data)
      if player:hasSkill(self)  then
        for _, move in ipairs(data)  do
          if move.to ~= nil and move.toArea == Card.PlayerHand  and move.skillName ~= self.name then
            local p = player.room:getPlayerById(move.to)
            if  (p:getNextAlive() == player or player:getNextAlive() == p) and not p:isNude()  then
              return true
            end
          end
        end
      end
    end,  
    on_cost = function(self, event, target, player, data)
      local room = player.room
      local targets = {}
      for _, move in ipairs(data) do
        if move.to ~= nil and move.toArea == Card.PlayerHand  and move.skillName ~= self.name then
          local p = player.room:getPlayerById(move.to)
          if  (p:getNextAlive() == player or player:getNextAlive() == p) and not p:isNude() then
            table.insertIfNeed(targets, move.to)
          end
        end
      end
      if #targets == 1 then
        if room:askForSkillInvoke(player, self.name, nil, "#jianzhong__1-invoke::"..targets[1]) then
          room:doIndicate(player.id, targets)
          self.cost_data = targets[1]
          return true
        end
      elseif #targets > 1 then
        local tos = room:askForChoosePlayers(player, targets, 1, 1, "#jianzhong__1-invoke-multi", self.name, true)
        if #tos > 0 then
          self.cost_data = tos[1]
          return true
        end
      end
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local from = room:getPlayerById(self.cost_data)
      local lastplayer = (player:getNextAlive() == from)
      local card = room:askForCardChosen(player, from, "he", self.name)
      room:obtainCard(player.id, card, false, fk.ReasonPrey,player.id,self.name)
      if player.dead or player:isNude() then return false end
      local to = player:getNextAlive()
      if lastplayer then
        to = player:getLastAlive()
      end
      if to == nil or to == player then return false end
      local id = room:askForCard(player, 1, 1, true, self.name, false, ".", "#jianzhong__1-choose::"..to.id)[1]
      room:obtainCard(to, id, false, fk.ReasonGive,player.id,self.name)
      player:drawCards(1, self.name)
    end,
  }
  chengui:addSkill(jianzhong__1)
 
  Fk:loadTranslationTable{
   
    ["jianzhong1"] = "剑冢1号",
    ["designer:jianzhong1"] = "妄生",
    ["jianzhong__1"] = "剑冢技能1",
    [":jianzhong__1"] = "当一名角色不以此法获得牌后，若其是你的上家或下家，你可以获得该角色的一张牌，然后交给你的下家或上家一张牌，最后你摸一张牌。",
    ["#jianzhong__1-invoke"] = "剑冢技能1：你可以获得 %dest 的一张牌",
    ["#jianzhong__1-invoke-multi"] = "剑冢技能1：你可以获得上家或下家的一张牌",
    ["#jianzhong__1-choose"] = "剑冢技能1：选择一张牌交给 %dest",
  
}
  
local jianzhong__hujinding = General(extension, "jianzhong2", "shu", 4)
local jianzhong__2 = fk.CreateTriggerSkill{
  name = "jianzhong__2",
  anim_type = "control",
  frequency = Skill.Compulsory,
  events = {fk.GameStart, fk.Damaged, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if event == fk.AfterCardsMove then
        local mark = player:getTableMark("jianzhong__2_target")
        if #mark == 0 then return false end
        local room = player.room
        for _, move in ipairs(data) do
          if move.to and table.contains(mark, move.to) and move.toArea == Card.PlayerHand then
            local p = room:getPlayerById(move.to)
            if not p.dead then
              local targets = table.filter(mark, function (pid)
                p = room:getPlayerById(pid)
                return not (p.dead or p:isKongcheng())
              end)
              if #targets > 0 then
                self.cost_data = targets
                return true
              end
            end
          end
        end
      elseif event == fk.Damaged then
        return target == player and player:hasSkill("jianzhong__2")
      elseif event == fk.GameStart then
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardsMove then
      for _, p in ipairs(self.cost_data) do
        local cards = room:getPlayerById(p):getCardIds{Player.Hand}
        if #cards == 0 then return false end
        room:obtainCard(player.id, table.random(cards), false, fk.ReasonPrey)
      end
    else
      local mark = player:getTableMark("jianzhong__2_target")
      local tos = table.map(table.filter(room.alive_players, function(p)
        return p ~= player and not table.contains(mark, p.id)
      end), Util.IdMapper)
      if #tos == 0 then return false end
        if event == fk.GameStart then 
          tos = room:askForChoosePlayers(player, tos, 1, 1, "#jianzhong__2-choose", self.name, false)
          table.insert(mark, tos[1])
          room:setPlayerMark(player, "jianzhong__2_target", mark)
          room:setPlayerMark(room:getPlayerById(tos[1]), "@@jianzhong__2", 1)
        elseif event == fk.Damaged then 
          tos = room:askForChoosePlayers(player, tos, 1, 1, "#jianzhong__2-choose", self.name, true)
          if #tos ~= 0 then     
            table.insert(mark, tos[1])
            room:setPlayerMark(player, "jianzhong__2_target", mark)
            room:setPlayerMark(room:getPlayerById(tos[1]), "@@jianzhong__2", 1) 
          else
          end       
        end
    end
  end,

  refresh_events = {fk.BuryVictim},
  can_refresh = function(self, event, target, player, data)
    return player:getMark("@@jianzhong__2") > 0 and table.every(player.room.alive_players, function(p)
    end)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@jianzhong__2", 0)
  end,
}
jianzhong__hujinding:addSkill(jianzhong__2)
Fk:loadTranslationTable{
  ["jianzhong2"] = "剑冢2号",
  ["jianzhong__2"] = "剑冢技能2",
  [":jianzhong__2"] = "锁定技，游戏开始时，你选择一名其他角色；当你受到伤害后，你可以选择一名其他角色。"..
  "当以此法选择的角色获得牌后，你获得其中所有一名角色的一张手牌。",
  ["#jianzhong__2-choose"] = "剑冢技能2：选择一名其他角色",
  ["@@jianzhong__2"] = "被剑冢",
}

local jianzhong__tengfanglan = General(extension, "jianzhong3", "wu", 4)
  local jianzhong__3 = fk.CreateTriggerSkill{
    name = "jianzhong__3",
    anim_type = "control",
    events = {fk.EventPhaseStart},
    can_trigger = function(self, event, target, player, data)
      return player:hasSkill(self) and target.phase == Player.Play
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local n = 4
      local to, targets, cards
      local luochong_map = {}
      repeat
        targets = table.map(table.filter(room.alive_players, function(p)
          return not p:isAllNude() end), Util.IdMapper)
        if #targets == 0 then break end
        targets = room:askForChoosePlayers(player, targets, 1, 1,
          "#jianzhong__3-choose:::"..tostring(n)..":"..tostring(n), self.name, true)
        if #targets == 0 then break end
        to = room:getPlayerById(targets[1])
        cards = room:askForCardsChosen(player, to, 1, n, "hej", self.name)
        room:throwCard(cards, self.name, to, player)
        luochong_map[to.id] = luochong_map[to.id] or 0
        luochong_map[to.id] = luochong_map[to.id] + #cards
        n = n - #cards
        if n <= 0 then break end
      until n == 0 or player.dead
    end,
  }
  jianzhong__tengfanglan:addSkill(jianzhong__3)
  Fk:loadTranslationTable{
    ["jianzhong3"] = "剑冢3号",
    ["jianzhong__3"] = "剑冢技能3",
    [":jianzhong__3"] = "每名角色摸牌阶段结束后，你可以弃置任意名角色区域内共计至多4张牌。",
    ["#jianzhong__3-choose"] = "剑冢技能3：你可以依次选择角色，弃置其区域内的牌（共计至多%arg张，还剩%arg2张）",
  
}

local jianzhong__yangqiu = General(extension, "jianzhong4", "qun", 4)

  local jianzhong__4 = fk.CreateActiveSkill{
    name = "jianzhong__4",
    anim_type = "control",
    card_num = 0,
    target_num = 1,
    prompt = "#jianzhong__4",
    mute = true,
    can_use = function(self, player)
      return player:hasSkill(self) 
    end,
    card_filter = Util.FalseFunc,
    target_filter = function(self, to_select, selected)
      return #selected == 0 and to_select ~= Self.id and not Fk:currentRoom():getPlayerById(to_select):isKongcheng() and Fk:currentRoom():getPlayerById(to_select):getMark("jianzhong__4-turn") == 0
    end,
    on_use = function(self, room, effect)
      local player = room:getPlayerById(effect.from)
      room:notifySkillInvoked(player, self.name)
      player:broadcastSkillInvoke(self.name, math.random(1, 2))
  
      local to = room:getPlayerById(effect.tos[1])
      if to:isKongcheng() then
        return
      end
  
      local ids, choice = U.askforChooseCardsAndChoice(
        player,
        to:getCardIds("h"),
        { "reveal", "not_reveal" },
        self.name,
        "#jianzhong__4-view::" .. to.id,
        nil,
        1,
        1
      )
  
      if choice == "reveal" then
        local toViewPlayers = table.filter(room.alive_players, function(p) return p ~= to end)
        if #toViewPlayers > 0 then
          for _, p in ipairs(toViewPlayers) do
            p:doNotify(
              "ShowCard",
              json.encode{
                from = player.id,
                cards = ids,
              }
            )
          end
          room:sendFootnote(ids, {
            type = "#jianzhong__4Reveal",
            from = player.id,
          })
        end
      end
  
      for i = 1, 999 do
        local idsDiscarded = room:askForDiscard(to, 1, 1, false, self.name, false, ".", "#jianzhong__4-discard:::" )
        if #idsDiscarded > 0 and idsDiscarded[1] == ids[1] then
          break
        elseif #idsDiscarded > 0 and idsDiscarded[1] ~= ids[1] then
        player:broadcastSkillInvoke("jianzhong__4", 4) 
      end
  
      if player:isAlive() and to:getHandcardNum() > player:getHandcardNum() then
        player:broadcastSkillInvoke("jianzhong__4", 4)
      elseif to:getHandcardNum() <= player:getHandcardNum() then 
        room:setPlayerMark(to, "jianzhong__4-turn",1)
      end
    end
    end,
  }
  jianzhong__yangqiu:addSkill(jianzhong__4)
  Fk:loadTranslationTable{
    ["jianzhong4"] = "剑冢4号",

    ["jianzhong__4"] = "剑冢技能4",
    [":jianzhong__4"] = "出牌阶段每名角色限一次，你可以观看一名其他角色的手牌并选择其中一张令除其外的角色观看，然后其重复弃置一张手牌，" ..
    "直至其弃置了你选择的牌。然后若其手牌数大于你，你视为未对其使用过。",
    ["#jianzhong__4"] = "剑冢技能4：你可观看一名其他角色的手牌，令其弃置手牌直到弃到你所选的牌",
    ["jianzhong__4_view"] = "剑冢技能4观看",
    ["reveal"] = "他人可观看",
    ["not_reveal"] = "不可观看",
    ["#jianzhong__4Reveal"] = "%from选择",
    ["#jianzhong__4-view"] = "剑冢技能4：当前观看的是 %dest 的手牌",
    ["#jianzhong__4-discard"] = "剑冢技能4：请弃置一张手牌，直到你弃置到“剑冢技能4”选择的牌",

    ["$saojian1"] = "虎豹豺狼、蚊蝇鼠蟑，按律，皆斩。",
    ["$saojian2"] = "蒙鹰犬之任，埽朝廷奸鄙。",
    ["$saojian3"] = "陛下，请假臣一月之期！",
    ["$jianzhong__44"] = "出生，你又藏了什么？",
    ["~yangqiu"] = "党人皆力锄奸宦而死，阳球之后，亦有志士。",

  }
  







local jianzhong__chengui = General(extension, "jianzhong5", "qun", 4)
  local jianzhong__5 = fk.CreateTriggerSkill{
    name = "jianzhong__5",
    anim_type = "control",
    events = {fk.AfterCardsMove},
    can_trigger = function(self, event, target, player, data)
      if player:hasSkill(self)  then
        for _, move in ipairs(data)  do
          if move.to ~= nil and move.toArea == Card.PlayerHand  and move.skillName ~= self.name then
            local p = player.room:getPlayerById(move.to)
            if p ~=player and  not p:isNude()  then
              return true
            end
          end
        end
      end
    end,  
    on_cost = function(self, event, target, player, data)
      local room = player.room
      local targets = {}
      for _, move in ipairs(data) do
        if move.to ~= nil and move.toArea == Card.PlayerHand  and move.skillName ~= self.name then
          local p = player.room:getPlayerById(move.to)
          if  p ~=player and  not p:isNude() then
            table.insertIfNeed(targets, move.to)
          end
        end
      end
      if #targets == 1 then
        if room:askForSkillInvoke(player, self.name, nil, "#jianzhong__5-invoke::"..targets[1]) then
          room:doIndicate(player.id, targets)
          self.cost_data = targets[1]
          return true
        end
      elseif #targets > 1 then
        local tos = room:askForChoosePlayers(player, targets, 1, 1, "#jianzhong__5-invoke-multi", self.name, true)
        if #tos > 0 then
          self.cost_data = tos[1]
          return true
        end
      end
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local from = room:getPlayerById(self.cost_data)
      local card = room:askForCardChosen(player, from, "he", self.name)
      room:obtainCard(player.id, card, false, fk.ReasonPrey,player.id,self.name)
      if player.dead or player:isNude() then return false end
      local tos = player.room:askForChoosePlayers(player, table.map(player.room:getOtherPlayers(player), Util.IdMapper), 1, 1,
      "#jianzhong__5", self.name, false)
      if #tos == 1 and tos ~= player  then 
      local to= room:getPlayerById(tos[1])
      local id = room:askForCard(player, 1, 1, true, self.name, false, ".", "#jianzhong__5-choose::"..to.id)[1]
      room:obtainCard(to, id, false, fk.ReasonGive,player.id,self.name)
      player:drawCards(1, self.name)
      end
    end,
  }
  jianzhong__chengui:addSkill(jianzhong__5)
 
  Fk:loadTranslationTable{
   
    ["jianzhong5"] = "剑冢5号",
    ["designer:jianzhong5"] = "妄生",
    ["jianzhong__5"] = "剑冢技能5",
    [":jianzhong__5"] = "当一名角色不以此法获得牌后，你可以获得该角色的一张牌，然后交给另一名其他角色一张牌，最后你摸一张牌。",
    ["#jianzhong__5-invoke"] = "剑冢技能5：你可以获得 %dest 的一张牌",
    ["#jianzhong__5-invoke-multi"] = "剑冢技能5：你可以获得 %dest 的一张牌",
    ["#jianzhong__5"] = "剑冢技能5：选择一名其他角色，并交给其",
    ["#jianzhong__5-choose"] = "剑冢技能5：选择一张牌交给 %dest",
  
}
  
local jianzhong6 = General(extension, "jianzhong6", "qun", 3)
  local jianzhong__6 = fk.CreateTriggerSkill{
  name = "jianzhong__6",
  anim_type = "control",
  events = {fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target ~= player  then
      return not player:isKongcheng()
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local card = room:askForCard(player, 1, 1, true, self.name, true, nil, '#jianzhong__6::' .. target.id)
    if #card > 0 then
      self.cost_data = card[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = self.cost_data
    room:moveCardTo(card, Player.Hand, target, fk.ReasonGive, self.name, nil, true, player.id)
    target:skip(Player.Play)
    target:skip(Player.draw)
  end
  }
  jianzhong6:addSkill(jianzhong__6)
  Fk:loadTranslationTable{
  ["jianzhong6"] = "剑冢6号",

  ["jianzhong__6"] = "剑冢技能6",
  [":jianzhong__6"] = "其他角色的回合开始时，你可以交给其一张牌，令其跳过其摸牌阶段和出牌阶段。",
  ["#jianzhong__6"] = "剑冢技能6: 你可以交给 %dest 一张牌，跳过其摸牌阶段和出牌阶段",
}

local jianzhong__zhouxuan = General(extension, "jianzhong7", "wei", 4)
local jianzhong__7 = fk.CreateTriggerSkill{
  name = "jianzhong__7",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
      for _, p in ipairs(room.alive_players) do
        if p ~= player and not p:isNude() then
          table.insert(targets, p.id)
        end
      end
      local to = room:askForChoosePlayers(player, targets, 1, 1, "#jianzhong__7-choose", self.name, true)
      if #to > 0 then
        self.cost_data = to[1]
        return true
      end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local p = room:getPlayerById(self.cost_data)
    local cards = room:askForDiscard(p, 2, 2, true, self.name, false, ".", "#jianzhong__7-discard:"..player.id)
    local x = Fk:getCardById(cards[1]).number
    if #cards == 2 then
      x = x + Fk:getCardById(cards[2]).number
    end
    if x > 10 and not p.dead then
      room:damage{
        from = player,
        to = p,
        damage = 1,
        damageType = fk.FireDamage,
        skillName = self.name,
      }
    end
end,

}
jianzhong__zhouxuan:addSkill(jianzhong__7)

Fk:loadTranslationTable{
  ["jianzhong7"] = "剑冢7号",

  ["jianzhong__7"] = "剑冢技能7",
  [":jianzhong__7"] = "你使用牌时，可以令一名其他角色弃置两张牌，若点数之和大于10，对其造成1点火焰伤害。",
  ["#jianzhong__7-choose"] = "剑冢技能7: 令一名其他角色弃置两张牌，若点数之和大于10，对其造成1点火焰伤害",
  ["#jianzhong__7-discard"] = "剑冢技能7：弃置2张牌，若点数之和大于10，%src 对你造成1点火焰伤害",

}

local jianzhong__wanglang = General(extension, "jianzhong8", "wei", 3)
local jianzhong__8 = fk.CreateActiveSkill{
  name = "jianzhong__8",
  anim_type = "control",
  card_num = 0,
  min_target_num = 1,
  max_target_num = 7,
  prompt = "#jianzhong__8-prompt",
  can_use = function(self, player)
    return not player:isKongcheng() 
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    return #selected < 7 and to_select ~= Self.id and Self:canPindian(Fk:currentRoom():getPlayerById(to_select))
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:sortPlayersByAction(effect.tos)
    local targets = table.map(effect.tos, Util.Id2PlayerMapper)
    local pindian = player:pindian(targets, self.name)
    for _, target in ipairs(targets) do
      local losers = {}
      if pindian.results[target.id].winner then
        if pindian.results[target.id].winner == player then
          table.insert(losers, target)
        else
          table.insert(losers, player)
        end
      else
        table.insert(losers, player)
        table.insert(losers, target)
      end
      for _, p in ipairs(losers) do
        if not player.dead then
          local choices = {  "jianzhong__8_draw" }
          if not p:isKongcheng() then
            table.insert(choices, "jianzhong__8_discard")
          end
          local choice = room:askForChoice(player, choices, "#jianzhong__8-choose", self.name)
          if choice == "jianzhong__8_draw" then
            player:drawCards(1, self.name)
          else
            room:askForDiscard(p, 1, 1, true, self.name, false, ".", "#jianzhong__8-discard::"..player.id)  
          end
        end
      end
    end
  end,
}
jianzhong__wanglang:addSkill(jianzhong__8)
Fk:loadTranslationTable{
  ["jianzhong8"] = "剑冢8号",
  ["jianzhong__8"] = "剑冢技能8",
  [":jianzhong__8"] = "出牌阶段，你可以用一张手牌与至多七名角色同时拼点，并依次结算拼点结果，然后你选择一项令没赢的角色执行：1.弃置一张牌；2.令你摸一张牌。",
  ["#jianzhong__8-choose"] = "剑冢技能8：你需选择一项",
  ["#jianzhong__8-discard"] = "剑冢技能8：你需弃置一张牌",
  ["#jianzhong__8-prompt"] = "剑冢技能8：你可以与至多七名角色同时拼点(选项描述有问题)",
  ["jianzhong__8_draw"] = "你摸一张牌",
  ["jianzhong__8_discard"] = "令其弃置一张牌",

}

local jianzhong__liubiao = General(extension, "jianzhong9", "qun", 3)
local jianzhong__9 = fk.CreateTriggerSkill{
  name = "jianzhong__9",
  anim_type = "control",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player ~= target and target.phase == Player.Finish and not target:isNude() then
      return #player.room.logic:getActualDamageEvents(1, function(e)
        local damage = e.data[1]
        return (damage.from == target and damage.to == player)
      end) == 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:askForCard(target, 1, 1, true, self.name, false, ".", "#jianzhong__9-give::"..player.id)
    room:obtainCard(player, cards[1], false, fk.ReasonGive)
  end,
}
jianzhong__liubiao:addSkill(jianzhong__9)
local jianzhong__9_2 = fk.CreateTriggerSkill{
  name = "jianzhong__9_2",
  anim_type = "control",
  frequency = Skill.Compulsory,
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return  player:hasSkill(self) and player == target 
  end,
  on_cost = function(self, event, target, player, data)
    local to = player.room:askForChoosePlayers(player, table.map(player.room:getOtherPlayers(player), Util.IdMapper)
    , 1, 1, "#jianzhong__9_2-choose:::", self.name, true)
    if #to > 0 then
      self.cost_data = {tos = to}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = player.room:getPlayerById(self.cost_data.tos[1])
    to:throwAllCards("h")
  end,
}
jianzhong__liubiao:addSkill(jianzhong__9_2)
Fk:loadTranslationTable{
  ["jianzhong9"] = "剑冢9号",
  ["jianzhong__9"] = "剑冢技能9",
  [":jianzhong__9"] = "锁定技，其他角色的结束阶段，若本回合其未对你造成过伤害，其交给你一张牌。",
  ["jianzhong__9_2"] = "剑冢技能9.2",
  [":jianzhong__9_2"] = "锁定技，当你受到伤害后，你可以弃置一名角色所有手牌。",
  ["#jianzhong__9-give"] = "剑冢技能9：你须交给 %dest 一张牌 ",
  ["#jianzhong__9_2-choose"] = "剑冢技能9.2：你须选择一名其他角色，令其弃置所有手牌",
  
}


local jianzhong__sunluyu = General(extension, 'jianzhong10', 'wu', 3, 3, General.Female)
local jianzhong__10 = fk.CreateTriggerSkill{
  name = "jianzhong__10",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target.phase == Player.Play and target ~= player and not target:hasSkill('jianzhong__10__zhixi', true) and not player:isNude()
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cards = room:askForDiscard(player, 1, 1, true, self.name, true, ".", "#jianzhong__10-invoke:" .. target.id, true)
    if #cards == 1 then
      self.cost_data = {tos = {target.id}, cards = cards}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = Fk:getCardById(self.cost_data.cards[1])
    room:throwCard(card, self.name, player, player)
    room:setPlayerMark(target, "jianzhong__10", 1)
    room:handleAddLoseSkills(target, 'jianzhong__10__zhixi', nil, true)
  end,

  refresh_events = { fk.TurnEnd },
  can_refresh = function(self, event, target, player, data)
    return target == player and target:getMark("jianzhong__10") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(target, "jianzhong__10", 0)
    room:handleAddLoseSkills(target, '-jianzhong__10__zhixi', nil, true)
  end,
}
local jianzhong__10_1 = fk.CreateTriggerSkill{
  name = "#jianzhong__10_1",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return
    target and
      player:hasSkill(self) and
      player.phase == Player.Start and
      player:usedSkillTimes(self.name) == 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, self.name)
  end,
}
jianzhong__10:addRelatedSkill(jianzhong__10_1)
jianzhong__sunluyu:addSkill(jianzhong__10)
jianzhong__sunluyu:addSkill("ol__mumu")
local jianzhong__10__zhixi = fk.CreateTriggerSkill{
  name = 'jianzhong__10__zhixi',
  frequency = Skill.Compulsory,

  refresh_events = { fk.PreCardUse, fk.HpChanged, fk.MaxHpChanged, fk.EventAcquireSkill, fk.EventLoseSkill},
  can_refresh = function(self, event, target, player, data)
    if player ~= target or player.dead or player.phase ~= Player.Play then return false end
    if event == fk.PreCardUse then
      return player:hasSkill(self, true) and player:getMark("jianzhong__10__zhixi_prohibit-phase") == 0
    elseif event == fk.HpChanged or event == fk.MaxHpChanged then
      return player:hasSkill(self, true) and player:getMark("jianzhong__10__zhixi_prohibit-phase") == 0
    elseif event == fk.EventPhaseStart then
      return player:hasSkill(self, true)
    elseif event == fk.EventAcquireSkill then
      return data == self and player.room:getTag("RoundCount")
    elseif event == fk.EventLoseSkill then
      return data == self
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.PreCardUse then
      if data.card.type == Card.TypeTrick then
        room:setPlayerMark(player, "jianzhong__10__zhixi_prohibit-phase", 1)
        room:setPlayerMark(player, "@jianzhong__10__zhixi-phase", {"jianzhong__10__zhixi_prohibit"})
      else
        local x = player:getMark("jianzhong__10__zhixi-phase") + 1
        room:setPlayerMark(player, "jianzhong__10__zhixi-phase", x)
        x = player.hp - x
        room:setPlayerMark(player, "@jianzhong__10__zhixi-phase", x > 0 and {"jianzhong__10__zhixi_remains", x} or {"jianzhong__10__zhixi_prohibit"})
      end
    elseif event == fk.HpChanged or event == fk.MaxHpChanged then
      local x = player.hp - player:getMark("jianzhong__10__zhixi-phase")
      room:setPlayerMark(player, "@jianzhong__10__zhixi-phase", x > 0 and {"jianzhong__10__zhixi_remains", x} or {"jianzhong__10__zhixi_prohibit"})
    elseif event == fk.EventPhaseStart then
      room:setPlayerMark(player, "@jianzhong__10__zhixi-phase", player.hp > 0 and {"jianzhong__10__zhixi_remains", player.hp} or {"jianzhong__10__zhixi_prohibit"})
    elseif event == fk.EventAcquireSkill then
      local phase_event = room.logic:getCurrentEvent():findParent(GameEvent.Phase, true)
      if phase_event == nil then return false end
      local end_id = phase_event.id
      local x = 0
      local use_trick = false
      room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
        local use = e.data[1]
        if use.from == player.id then
          if use.card.type == Card.TypeTrick then
            use_trick = true
            return true
          end
          x = x + 1
        end
        return false
      end, end_id)
      if use_trick then
        room:setPlayerMark(player, "jianzhong__10__zhixi_prohibit-phase", 1)
        room:setPlayerMark(player, "@jianzhong__10__zhixi-phase", {"jianzhong__10__zhixi_prohibit"})
      else
        room:setPlayerMark(player, "jianzhong__10__zhixi-phase", x)
        x = player.hp - x
        room:setPlayerMark(player, "@jianzhong__10__zhixi-phase", x > 0 and {"jianzhong__10__zhixi_remains", x} or {"jianzhong__10__zhixi_prohibit"})
      end
    elseif event == fk.EventLoseSkill then
      room:setPlayerMark(player, "@jianzhong__10__zhixi-phase", 0)
    end
  end,
}
local jianzhong__10__zhixip = fk.CreateProhibitSkill{
  name = '#jianzhong__10__zhixi_prohibit',
  prohibit_use = function(self, player)
    return player:hasSkill(jianzhong__10__zhixi) and player.phase == Player.Play and
    (player:getMark("jianzhong__10__zhixi_prohibit-phase") > 0 or player:getMark("jianzhong__10__zhixi-phase") >= player.hp)
  end,
}
local jianzhong__10__ty__zhixi = fk.CreateTriggerSkill{
  name = "#jianzhong__10__ty__zhixi",
  anim_type = "negative",
  frequency = Skill.Compulsory,
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(jianzhong__10__zhixi) and player.phase == Player.Play and not player:isKongcheng() and
      data.card.trueName == "slash"
  end,

  on_use = function(self, event, target, player, data)
    player.room:askForDiscard(player, 1, 1, false, jianzhong__10__zhixi.name, false)
  end,
}
jianzhong__10__zhixi:addRelatedSkill(jianzhong__10__zhixip)
jianzhong__10__zhixi:addRelatedSkill(jianzhong__10__ty__zhixi)

jianzhong__sunluyu:addRelatedSkill(jianzhong__10__zhixi)
Fk:loadTranslationTable{
  ["jianzhong10"] = '剑冢10号',
  ['jianzhong__10'] = '剑冢技能10',
  [':jianzhong__10'] = '场上角色的阶段开始时，你摸一张牌，其他角色出牌阶段开始时，' ..
    '你可以弃置一张牌，令该角色于此回合内拥有〖止息〗。' ,
  ['#jianzhong__10-invoke'] = '魅步：是否弃置一张牌让 %src 本回合获得技能“止息”？',
  ["jianzhong__10__zhixi"] = "止息",
  [":jianzhong__10__zhixi"] = "锁定技，出牌阶段你可至多使用X张牌，你使用锦囊牌后，不能再使用牌，当你使用【杀】时，需弃置一张手牌（X为你的体力值）。",
  ['@jianzhong__10__zhixi-phase'] = '止息',
  ["jianzhong__10__zhixi_remains"] = "剩余",
  ["jianzhong__10__zhixi_prohibit"] = "不能出牌",
}
local jianzhong__fuhuanghou = General(extension, "jianzhong11", "qun", 3, 3, General.Female)
local jianzhong__11_1 = fk.CreateTriggerSkill{
  name = "jianzhong__11_1",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player:usedSkillTimes(self.name) == 0 and
    target and not target.dead and target ~= player and target.phase == Player.Start and
     player:canPindian(target)
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, data, "#jianzhong__11_1-invoke::"..target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, {target.id})
    local pindian = player:pindian({target}, self.name)
    if pindian.results[target.id].winner == player then
      room:addPlayerMark(target, "@@jianzhong__11_1_prohibit-turn")
    elseif not player.dead then
      local slash = Fk:cloneCard("slash")
      if not target.dead and not player.dead and not target:prohibitUse(slash) and not target:isProhibited(player, slash) then
        room:useVirtualCard("slash", nil, target, player, self.name, true)
      end
    end
  end
}

local jianzhong__11_1_prohibit = fk.CreateProhibitSkill{
  name = "#jianzhong__11_1_prohibit",
  is_prohibited = function(self, from, to, card)
    return from:getMark("@@jianzhong__11_1_prohibit-turn") > 0 and from ~= to
  end,
}

local jianzhong__11_1_delay = fk.CreateTriggerSkill{
  name = "#jianzhong__11_1_delay",
  events = {fk.PindianResultConfirmed},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not player.dead and data.from == player and data.winner and data.winner ~= player and
        data.toCard and player.room:getCardArea(data.toCard) == Card.Processing then
      local parentPindianEvent = player.room.logic:getCurrentEvent():findParent(GameEvent.Pindian, true)
      if parentPindianEvent then
        local pindianData = parentPindianEvent.data[1]
        return pindianData.reason == jianzhong__11_1.name
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    if not player.dead and data.toCard and player.room:getCardArea(data.toCard) == Card.Processing then
      player.room:obtainCard(player, data.toCard, true, fk.ReasonJustMove)
    end
  end,
}

jianzhong__11_1:addRelatedSkill(jianzhong__11_1_delay)
jianzhong__11_1:addRelatedSkill(jianzhong__11_1_prohibit)

jianzhong__fuhuanghou:addSkill(jianzhong__11_1)

local jianzhong__11_2 = fk.CreateTriggerSkill{--
  name = "jianzhong__11_2",
  anim_type = "defensive",
  events = {fk.TargetConfirming},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and data.card.trueName == "slash" then
      return #player.room:getOtherPlayers(player) > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.map(player.room:getOtherPlayers(player), Util.IdMapper)
    local to = room:askForChoosePlayers(player, targets, 1, 1, "#jianzhong__11_2-choose", self.name, true)
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    --FIXME: cant use "^slash|.|.|.|.|basic"!!
    local ids = table.filter(to:getCardIds("h"),
      function(id) return Fk:getCardById(id).type == Card.TypeBasic and Fk:getCardById(id).trueName ~= "slash"
    end)
    local card = room:askForCard(to, 1, 1, false, self.name, true, tostring(Exppattern{ id = ids }), "#jianzhong__11_2-give::"..player.id)
    if #card > 0 then
      room:moveCardTo(card, Player.Hand, player, fk.ReasonGive, self.name, nil, true, to.id)
    else
      AimGroup:addTargets(room, data, to.id)
      AimGroup:setTargetDone(data.tos, to.id)
    end
  end,
}
jianzhong__fuhuanghou:addSkill(jianzhong__11_2)
Fk:loadTranslationTable{
  ["jianzhong11"] = "剑冢11号",

  ["jianzhong__11_1"] = "剑冢技能11.1",
  ["#jianzhong__11_1"] = "剑冢技能11.1",
  [":jianzhong__11_1"] = "其他角色的准备阶段，你可与其拼点。若你赢，其本回合无法使用牌指定除其以外的角色为目标；若你没赢，其视为对你使用一张【杀】，然后其本回合不能对你使用牌。",
  ["#jianzhong__11_1-invoke"] = "剑冢技能11.1：你可以与 %dest 拼点，若赢则其本回合使用牌只能指定自己为目标",
  ["@@jianzhong__11_1_prohibit-turn"] = "恐惧",
  ["@@jianzhong__11_1_-turn"] = "恐惧",
  ["jianzhong__11_2"] = "剑冢技能11.2",
  [":jianzhong__11_2"] = "当你成为【杀】的目标时，你可以令一名其他角色交给你一张除【杀】以外的基本牌，否则也成为此【杀】的目标。",
  ["#jianzhong__11_2-choose"] = "剑冢技能11.2：令一名其他角色交给你一张不为【杀】的基本牌，否则其成为此【杀】额外目标",
  ["#jianzhong__11_2-give"] = "剑冢技能11.2：你需交给 %dest 一张不为【杀】的基本牌，否则成为此【杀】额外目标",

}












  return extension  